﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.View.Templates;
using VideoRentalShop.Controller;

namespace VideoRentalShop.View.Panel.RentStepPanel
{
    /// <summary>
    /// Logika interakcji dla klasy ChooseMoviesStepPanel.xaml
    /// </summary>
    public partial class ChooseMoviesStepPanel : StepPanelContent
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;


        public ChooseMoviesStepPanel()
        {
            InitializeComponent();
            opt24Hours.IsChecked = true;
        }


        private void RefreshCombobox()
        {
            cmbMovie.Items.Clear();

            foreach (var movie in _controller.Data.Movie.GetAll().Where(x => x.Avaiable > 0).OrderBy(x => x.Title).Select(x => new { Title = x.Title, Id = x.Id }))
            {
                cmbMovie.Items.Add(new Label { Content = movie.Title, Tag = movie.Id });
            }
        }

        public override void ClearForm()
        {
            RefreshCombobox();
            cmbMovie.SelectedIndex = -1;
            opt24Hours.IsChecked = true;
            lstMovies.Items.Clear();
        }

        public override bool ValidateForm()
        {
            if (lstMovies.Items.Count == 0)
            {
                MessageBox.Show("Wymagane jest wybranie co najmniej jednego filmu do oddania.", "Uzupełnij dane");
                return false;
            }

            var item = _item as ClientOrder;
            var heldMoviesCount = 0;
            if (item != null)
            {
                heldMoviesCount = item.HeldMovies.Count();
                foreach (dynamic movie in lstMovies.Items)
                {
                    if (item.HeldMovies.Contains((long)movie.MovieId))
                    {
                        MessageBox.Show(string.Format("Wybrany klient posiada już film o nazwie {0}.", movie.Title), "Błąd");
                        return false;
                    }
                }
            }
            if (lstMovies.Items.Count + heldMoviesCount > 3)
            {
                var result = MessageBox.Show("Przekroczono maksymalną ilość wypożyczanych filmów. Czy chesz kontynuować?", "Ostrzeżenie", MessageBoxButton.YesNo, MessageBoxImage.Warning);

                if (result != MessageBoxResult.Yes)
                {
                    return false;
                }
            }

            return true;
        }

        public override void UpdateItem()
        {
            var item = _item as ClientOrder;
            if (item != null)
            {
                var movies = new List<MovieOrderInfo>();

                foreach (dynamic lstItem in lstMovies.Items)
                {
                    var time = lstItem.Time;
                    var id = lstItem.MovieId;

                    movies.Add(new MovieOrderInfo { MovieId = id, Time = time });
                }

                item.Movies = movies;
                _item = item;
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var idx = cmbMovie.SelectedIndex;
            if (idx == -1)
            {
                MessageBox.Show("Wymagane jest uprzednie wybranie filmu z listy.");
                return;
            }

            var lbl = (Label)cmbMovie.SelectedItem;
            var movieId = (long)(lbl.Tag);
            var title = lbl.Content;

            if (HasMovie(movieId))
            {
                MessageBox.Show("Wybrany film znajduje się już na liście.");
                return;
            }
            var item = _item as ClientOrder;
            if (item != null)
            {
                if (item.HeldMovies.Contains(movieId))
                {
                    MessageBox.Show(string.Format("Wybrany klient posiada już film o nazwie {0}.", title), "Błąd");
                    return;
                }
            }

            var time = 24;
            if (opt48Hours.IsChecked == true) time = 48;
            if (opt72Hours.IsChecked == true) time = 72;

            lstMovies.Items.Add(new { Time = time, TimeStr = string.Format("{0}h", time), Title = title, MovieId = movieId });
        }

        private bool HasMovie(long movieId)
        {
            foreach (dynamic item in lstMovies.Items)
            {
                var id = item.MovieId;
                if (id == movieId)
                {
                    return true;
                }
            }

            return false;
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (lstMovies.SelectedItems.Count > 0)
            {
                var selectedItems = lstMovies.SelectedItems;
                for (var i = 0; i < selectedItems.Count; ++i)
                {
                    lstMovies.Items.Remove(selectedItems[0]);
                }
            }
        }
    }
}
